package org.shuma.goods.controller;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import lombok.extern.slf4j.Slf4j;
import org.shuma.goods.constant.RegularConstant;
import org.shuma.goods.group.AuthGroup;
import org.shuma.goods.utils.BeanCopyUtils;
import org.shuma.user.facade.UserFacade;
import org.shuma.user.utils.Result;
import org.shuma.user.vo.AuthVo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;

/**
 * @author zhao haitao
 * @date 2024/06/17 1:18 AM
 */
@Controller
@RestController
@Validated
@Slf4j
public class AuthController {
    @SofaReference(interfaceType = UserFacade.class, uniqueId = "${service.user.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private UserFacade authService;

    @GetMapping("/code")
    public Result sendVerifyCode(@RequestParam("email") @Valid @Email(regexp = RegularConstant.EMAIL, message = "邮箱格式不正确") @NotNull String email) {
        log.info("发送验证码:{}", email);
        return authService.sendVerifyCode(email);
    }

    @PostMapping("/register")
    public Result register(@RequestBody @Validated(AuthGroup.register.class) AuthVo auth) {
        log.info("注册用户:{}", auth);
        return authService.register(BeanCopyUtils.copyBean(auth, org.shuma.user.vo.AuthVo.class));
    }

    @PostMapping("/login")
    public Result login(@RequestBody @Validated(AuthGroup.login.class) AuthVo auth) {
        log.info("用户登录:{}", auth);
        return authService.login(BeanCopyUtils.copyBean(auth, org.shuma.user.vo.AuthVo.class));
    }

    @PostMapping("/logout")
    public Result logout(@RequestParam("token") @NotNull String token) {
        log.info("用户登出:{}", token);
        return authService.logout(token);
    }

    @GetMapping("/token")
    public Result getNewToken(@RequestParam("token") @NotNull String token) {
        log.info("获取新token:{}", token);
        return authService.getNewToken(token);
    }
}
